﻿// 1028 人口普查.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

#include <iostream>
#include<cstdio>
#pragma warning( disable : 4996)
using namespace std;

struct people {
	int year;
	int month;
	int day;
	string name;
};

bool greater_(people p1, people p2) {
	return p1.year == p2.year ? p1.month == p2.month ? p1.day > p2.day:p1.month > p2.month:p1.year > p2.year;
}

bool isvalid(people p) {
	//2012 年 9 月 6 日之前的都要过滤掉
	people pmax, pmin;
	pmax.year = 1814;
	pmax.month = 9;
	pmax.day = 6;
	pmin.year = 2014;
	pmin.month = 9;
	pmin.day = 6;
	if (greater_(pmax,p)) {
		return false;
	}
	if (greater_(p,pmin)) {
		return false;
	}
	return true;
}

int main()
{
	int n;
	cin >> n;
	//构造最年轻和最年老的人
	people pmax, pmin;
	pmax.year = 2014;
	pmax.month = 9;
	pmax.day = 6;
	pmin.year = 1814;
	pmin.month = 9;
	pmin.day = 6;
	int thisyear, thismonth, thisday;
	string thisname;
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		people tmp;
		cin >> thisname;
		scanf("%d/%d/%d", &thisyear, &thismonth, &thisday);
		tmp.year = thisyear;
		tmp.month = thismonth;
		tmp.day = thisday;
		tmp.name = thisname;
		if (isvalid(tmp)) {
			cnt++;
		}
		if (isvalid(tmp)&&greater_(pmax,tmp)) {
			pmax = tmp;
		}
		if (isvalid(tmp)&&greater_(tmp, pmin)) {
			pmin = tmp;
		}
	}
	if (cnt != 0) {
		cout << cnt << " " << pmax.name << " " << pmin.name;
	}
	else {
		cout << "0";
	}
	
	return 0;
}